package com.mingqi.enetconnected.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.primary.mytask.adapter.AddImgAdapter;
import com.mingqi.enetconnected.view.decoration.DividerGridItemDecoration;

public class HorizontalImageLayout extends LinearLayout {

    private RecyclerView rvImage;
    private TakePhoto takePhoto;
    private AddImgAdapter adapter;

    public interface TakePhoto {
        public void takePhoto();
    }

    public HorizontalImageLayout(Context context) {
        this(context, null, 0);
    }

    public HorizontalImageLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalImageLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_horizontal_image_layout, this);

        rvImage = findViewById(R.id.rv_image);

        initData(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    private void initData(final Context cxt) {

        adapter = new AddImgAdapter(null);
        TextView tx = new TextView(cxt);
        tx.setText("添加照片");
        tx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (takePhoto != null)
                    takePhoto.takePhoto();
            }
        });
        adapter.addFooterView(tx);
        rvImage.setAdapter(adapter);
        rvImage.addItemDecoration(new DividerGridItemDecoration(rvImage.getContext()));
        rvImage.setLayoutManager(new GridLayoutManager(rvImage.getContext(), 4));
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                Toast.makeText(cxt, "xxxxxxxxxx"+position, Toast.LENGTH_LONG).show();

            }
        });
    }

    public void setTakePhoto(TakePhoto takePhoto) {
        this.takePhoto = takePhoto;
    }

    public void refresh(String data) {
        if (adapter != null) {
            adapter.addData(data);
        }
    }

}
